<?php

namespace App\Http\Controllers\Api;

use App\Activity;
use App\ActivityImg;
use App\AnswerSheet;
use App\Http\Services\FileManager;
use App\Http\Services\ToolsManager;
use App\Http\Services\UserManager;
use App\User;
use App\UserActivity;
use App\UserAnswer;
use App\UserInfo;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ActivityController extends Controller
{
    public function activitySignUp(Request $request){
        $realName   = !is_null($request->get('real_name')) ? $request->get('real_name') : '';
        $phone      = !is_null($request->get('phone')) ? $request->get('phone') : '';
        $photoId    = !is_null($request->get('photo_id')) ? $request->get('photo_id') : 0;
        $activityId = !is_null($request->get('activity_id')) ? $request->get('activity_id') : 1;

        /** @var Activity $thisActivity */
        $thisActivity = Activity::whereId($activityId)->whereStatus(1)->first();
        if(!$thisActivity)
            return response()->json(['level' => 'error','message' => '活动不存在']);

        if($realName == '')
            return response()->json(['level' => 'error','message' => '请输入姓名']);

        if($phone == '')
            return response()->json(['level' => 'error','message' => '请输入手机号']);

        if(!preg_match("/^1(3|4|5|6|7|8|9)\d{9}$/",$phone))
            return response()->json(['level' => 'error','message' => '手机号格式不正确']);

        if($photoId == 0)
            return response()->json(['level' => 'error','message' => '请传入头像']);

        /** @var User $phoneIsExist */
        $phoneIsExist = User::wherePhone($phone)->first();
        if(!$phoneIsExist){
            $phoneIsExist = new User();
            $phoneIsExist->phone         = $phone;
            $phoneIsExist->save();

            $phoneIsExist->user_key      = md5($phoneIsExist->id.rand(1000,9999).time());
            $phoneIsExist->save();

            $userInfo = new UserInfo();
            $userInfo->user_id     = $phoneIsExist->id;
            $userInfo->realname    = $realName;
            $userInfo->photo_id    = $photoId;
            $userInfo->save();

            $newUserActivity = new UserActivity();
            $newUserActivity->user_id       = $phoneIsExist->id;
            $newUserActivity->activity_id   = $thisActivity->id;
            $newUserActivity->status        = 1;
            $newUserActivity->save();
        }else{
            if(is_null($phoneIsExist->user_key) || $phoneIsExist->user_key == ''){
                $phoneIsExist->user_key      = md5($phoneIsExist->id.rand(1000,9999).time());
                $phoneIsExist->save();
            }

            $newUserActivity = UserActivity::whereUserId($phoneIsExist->id)->whereActivityId($thisActivity->id)->whereStatus(1)->first();
            if(!$newUserActivity){
                $newUserActivity = new UserActivity();
                $newUserActivity->user_id       = $phoneIsExist->id;
                $newUserActivity->activity_id   = $thisActivity->id;
                $newUserActivity->status        = 1;
                $newUserActivity->save();
            }
        }

        $token = CryptoJSAES::encrypt($phoneIsExist->user_key,env("AES_KEY"));

        return response()->json(['level' => 'success','message' => '报名成功','data' => $token]);
    }

    public function getActivityInfo(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        /** @var Activity $activityInfo */
        $activityInfo = Activity::whereId(1)->first();
        if($activityInfo){
            /** @var ActivityImg $activityImg */
            $activityImgList = ActivityImg::whereActivityId($activityInfo->id)->orderBy('created_at','desc')->get();
            $activityImgUrlArr = [];
            if(count($activityImgList) > 0){
                foreach($activityImgList as$activityImg){
                    $activityImgUrl = FileManager::getImgUrl($activityImg->img_id);
                    array_push($activityImgUrlArr,$activityImgUrl);
                }
            }

            $activityInfo->activity_banner_list = $activityImgUrlArr;

            $signUpNum = UserActivity::whereStatus(1)->count();

            $activityInfo->sign_up_num = $signUpNum;

            if($user){
                $isJoin = UserActivity::whereUserId($user->id)->whereStatus(1)->first();
                if($isJoin){
                    $activityInfo->is_join = 1;
                }else{
                    $activityInfo->is_join = 0;
                }
            }else{
                $activityInfo->is_join = null;
            }
        }

        $is_receive = 0;

        /** @var AnswerSheet $answerSheetTwo */
        $answerSheetTwo = AnswerSheet::whereActivityId(1)->whereSort(2)->whereStatus(1)->first();
        if($answerSheetTwo){
            if($user){
                $userAnswerOneTwoThree = UserAnswer::whereAsId($answerSheetTwo->id)->whereStatus(2)->orderBy('score','desc')->orderBy('submit_time','asc')->take(3)->lists('user_id')->toArray();
                if(in_array($user->id,$userAnswerOneTwoThree)){
                    $is_receive = 1;
                }
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $activityInfo,'is_receive' => $is_receive]);
    }

//    public function jiashuju1(Request $request){
//
//        $b = 101;
//        for($a = 0; $a <= 50; $a++){
//            $b = $b -1;
//
//            $phone = ToolsManager::getTool(7);
//
//            $newUser = new User();
//            $newUser->phone = $phone.rand(0000,9999);
//            $newUser->save();
//
//            $newUser->user_key      = md5($newUser->id.rand(1000,9999).time());
//            $newUser->save();
//
//            $newUserInfo = new UserInfo();
//            $newUserInfo->user_id    =  $newUser->id;
//            $newUserInfo->realname   = '虎'.rand(0000,9999);
//            $newUserInfo->photo_id   = 8;
//            $newUserInfo->save();
//
//            $newUserActivity = new UserActivity();
//            $newUserActivity->user_id      = $newUser->id;
//            $newUserActivity->activity_id  = 1;
//            $newUserActivity->save();
//
//            $newUserAnswer = new UserAnswer();
//            $newUserAnswer->user_id     = $newUser->id;
//            $newUserAnswer->as_id       = 1;
//            $newUserAnswer->status      = 2;
//            $newUserAnswer->submit_time = date("Y-m-d H:i:s",time());
//            $newUserAnswer->score       = $b;
//            $newUserAnswer->save();
//        }
//    }
//
//    public function jiashuju(){
//        $b = 101;
//
//        $userAnswerTwoList = UserAnswer::whereAsId(1)->whereStatus(2)->orderBy('score','desc')->orderBy('submit_time','asc')->take(30)->lists('user_id')->toArray();
//
//        if(count($userAnswerTwoList)){
//            foreach($userAnswerTwoList as $userAnswerTwo){
//                $b = $b -1;
//                $newUserAnswer = new UserAnswer();
//                $newUserAnswer->user_id     = $userAnswerTwo;
//                $newUserAnswer->as_id       = 2;
//                $newUserAnswer->status      = 2;
//                $newUserAnswer->submit_time = date("Y-m-d H:i:s",time());
//                $newUserAnswer->score       = $b;
//                $newUserAnswer->save();
//            }
//        }
//
//    }

}
